## This function takes in the (1) dataframe,
## (2) type of sample (usually subgroup_nowave to do the partisan grouped across waves analysis),
## (3) subgroup (usually party or wave, now deprecated because this analysis is no longer in the paper),
## and (4) weights (only relevant for dfR).

## It outputs a dataframe of cleaned regression estimates, to be used in visualize_means.R.


estimate_means <- function(.data,
                           .sample = c("all_respondents", "did", "subgroup", "subgroup_nowave"),
                           .byvar = NULL,
                           .weight = NULL){
  
  main_values = sym(names(.data)[str_detect(names(.data), "_values")])
  main_group = sym(names(.data)[str_detect(names(.data), "_dv")])
  
  if (.sample == "all_respondents" | .sample == "subgroup_nowave"){
    out0 = .data %>% 
      filter(!!main_group == "Main")
  } else if (.sample == "did"){
    out0 = .data %>%
      filter(!!main_group == "Main" & Party %in% c("Democrat", "Republican") & !is.na(Wave))
  } else if (.sample == "subgroup"){
    out0 = .data %>% 
      filter(!!main_group == "Main" & Wave %in% c("Pre", "Post"))
  }
  
  
  if(.sample == "all_respondents"){
    
    out1 <- out0 %>% 
      group_by(Error) %>% 
      summarise(tidy(lm_robust(!!main_values ~ 1, clusters = ResponseId, data = cur_data())), 
                .groups = "drop") %>% 
      mutate(model = "All")
  } else if (.sample == "did"){
    
    # This is for the pre, during, and the post
    out1a <- out0 %>% 
      group_by(Party, Wave, Error) %>% 
      summarise(tidy(lm_robust(!!main_values ~ 1, clusters = ResponseId, data = cur_data())), 
                .groups = "drop") 
    
    # This is for Post - Pre (by party)
    out1b <- out0 %>% 
      filter(Wave %in% c("Pre", "Post")) |> 
      group_by(Party, Error) %>% 
      summarise(tidy(lm_robust(!!main_values ~ Wave, clusters = ResponseId, data = cur_data())), 
                .groups = "drop") %>% 
      filter(term != "(Intercept)") %>% 
      mutate(Wave = "Difference in Wave")
    
    # This is for Republican - Democrat (by wave)
    out1c <- out0 %>% 
      group_by(Wave, Error) %>% 
      summarise(tidy(lm_robust(!!main_values ~ Party, clusters = ResponseId, data = cur_data())), 
                .groups = "drop") %>% 
      filter(term != "(Intercept)") %>% 
      mutate(Party = "Difference in Party")
    
    # This is for Differences in Differences
    out1d <- out0 %>% 
      filter(Wave %in% c("Pre", "Post")) |> 
      group_by(Error) %>% 
      summarise(tidy(lm_robust(!!main_values ~ Party*Wave, clusters = ResponseId, data = cur_data())), 
                .groups = "drop") %>% 
      filter(str_detect(term, ":")) %>% 
      mutate(Wave = "Difference in Differences",
             Party = "Difference in Differences")
    
    out1 <- rbind(out1a, out1b, out1c, out1d)
    
  } else if (.sample == "subgroup"){
    
    # This is for the pre and the post
    out1a <- out0 %>% 
      group_by({{.byvar}}, Wave, Error) %>% 
      summarise(tidy(lm_robust(!!main_values ~ 1, clusters = ResponseId, data = cur_data())), 
                .groups = "drop") 
    
    # This is for Post - Pre (by party)
    out1b <- out0 %>%
      group_by({{.byvar}}, Error) %>%
      summarise(tidy(lm_robust(!!main_values ~ Wave, clusters = ResponseId, data = cur_data())),
                .groups = "drop") %>%
      filter(term != "(Intercept)") %>%
      mutate(Wave = "Difference in Wave")
    
    out1 <- rbind(out1a, out1b)
    
  }  else if (.sample == "subgroup_nowave"){
    
    # This is for the party
    out1 <- out0 %>% 
      group_by({{.byvar}}, Error) %>% 
      summarise(tidy(lm_robust(!!main_values ~ 1, clusters = ResponseId, data = cur_data(),
                               weights = {{.weight}})), 
                .groups = "drop") 
    
    
  }
  
  return(out1)
  
  
  
}

# estimate_byvar <- function(.df, .byvar){
#   map_dfr(.df %>% distinct({{.byvar}}) %>% pull() %>% as.character,
#           ~estimate_means(.df %>% filter({{.byvar}} == .x), .model = "abstract",
#                           "all_respondents") %>% 
#             mutate(byvar = .x))
# }